package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author：郑谦
 * @时间：2017-4-10 下午4:02:27
 * @类名：QianbaoActivity.java
 * @功能: 个人中心-钱包
 */

public class QianbaoActivity extends Activity {

    HttpTools httptools = null;
    //	private TextView tv_fanhui;
    private TextView tv_yue;
    /**
     * 充值记录
     */
    private Button btn_chongzhi;
    /**
     * 消费记录
     */
    private Button btn_xiaofei;
    /**
     * 退款记录
     */
    private Button btn_tuikuan;
    private ImageButton ib_fanhui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_qianbao);
        initRes();
    }

    private void initRes() {
        tv_yue = (TextView) findViewById(R.id.tv_yue);
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui);
        btn_chongzhi = (Button) findViewById(R.id.btn_chongzhi);
        btn_xiaofei = (Button) findViewById(R.id.btn_xiaofei);
        btn_tuikuan = (Button) findViewById(R.id.btn_tuikuan);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_chongzhi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(QianbaoActivity.this, ChongzhiActivity.class);
                startActivity(intent);
            }
        });
        btn_xiaofei.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(QianbaoActivity.this, XiaofeiActivity.class);
                startActivity(intent);
            }
        });
        btn_tuikuan.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(QianbaoActivity.this, TuikuanActivity.class);
                startActivity(intent);
            }
        });
        Personal();
    }

    /**
     * 获取余额
     */
    private void Personal() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        Log.i("zq", "token---" + token);
        if (httptools == null) {
            httptools = new HttpTools(QianbaoActivity.this);
        }
        httptools.person(token, new JsonPerson());
    }

    public class JsonPerson extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("zq", response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    String balance = object.getString("balance");
//					tv_yue.setText(""+(int)(Double.parseDouble(balance)*10));
                    tv_yue.setText(balance);
                } else {
                    Toast.makeText(QianbaoActivity.this, msg, Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


}
